﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Blade.IO;
using System.IO;

namespace Blade.Metadata.PeLoader {
	sealed class CliHeap<T> : Dictionary<long, T> {
		public CliHeap(CursorStream stream, Func<BinaryReader, T> readOne)
			: this(stream, delegate(BinaryReader s, out T obj) {
				if (s.Position() < s.Length()) {
					obj = readOne(s);
					return true;
				} else {
					obj = default(T);
					return false;
				}
			}) { }
		public CliHeap(CursorStream stream, TryFunc<BinaryReader, T> readOne) {
			var reader = stream.AsBinaryReader();
			while (true) {
				var position = stream.Position;
				T obj;
				if (!readOne(reader, out obj))
					break;
				Add(position, obj);
			}
		}
	}
}
